﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DTO;
using System.Data.SqlClient;
using System.Data;

namespace DAO
{
    public class CT_PhieuNhapKhoDAO
    {
        public List<CT_PhieuNhapKhoDTO> LayChiTietPhieuNhapKhoTheoMa(int mapnk)
        {
            List<CT_PhieuNhapKhoDTO> listCTPNK = new List<CT_PhieuNhapKhoDTO>();
            SqlConnection conn = null;
            SqlDataReader reader = null;
            try
            {
                String sql = "Select a.*, b.Ten, b.DonViTinh from CTPhieuNhapKho a, NguyenVatLieu_SanPhamGiaiKhat b WHERE a.MaNVL = b.MaNVL AND MaPhieuNK = " + mapnk;

                conn = DataProvider.ConnectDB();
                SqlCommand cm = new SqlCommand(sql, conn);
                reader = cm.ExecuteReader();
                while (reader.Read())
                {
                    CT_PhieuNhapKhoDTO pnkDTO = new CT_PhieuNhapKhoDTO();
                    pnkDTO.MaPhieuNK = reader.GetInt32(0);
                    pnkDTO.MaNVL = reader.GetInt32(1);
                    pnkDTO.SoLuong = reader.GetInt32(2);
                    pnkDTO.DonGiaNhap = Convert.ToDouble(reader.GetDecimal(3));
                    pnkDTO.Ten = reader.GetString(4);
                    pnkDTO.DonViTinh = reader.GetString(5);
                    listCTPNK.Add(pnkDTO);
                }
            }
            catch (Exception ex)
            { String e = ex.ToString(); }
            finally
            { conn.Close(); }
            return listCTPNK;
        }

        public String ThemChiTietPhieuNhapKho(CT_PhieuNhapKhoDTO ctpdcDTO)
        {
            String mess = "";
            SqlConnection conn = null;
            try
            {
                conn = DataProvider.ConnectDB();
                String sql = "INSERT INTO CTPhieuNhapKho(MaPhieuNK, MaNVL, SoLuong, DonGiaNhap) ";
                sql += "VALUES(@MaPhieuNK, @MaNVL, @SoLuong, @DonGiaNhap)";
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = sql;
                SqlParameter para;
                para = cmd.Parameters.Add("@MaPhieuNK", SqlDbType.Int);
                para.Value = ctpdcDTO.MaPhieuNK;
                para = cmd.Parameters.Add("@MaNVL", SqlDbType.Int);
                para.Value = ctpdcDTO.MaNVL;
                para = cmd.Parameters.Add("@SoLuong", SqlDbType.Int);
                para.Value = ctpdcDTO.SoLuong;
                para = cmd.Parameters.Add("@DonGiaNhap", SqlDbType.Decimal);
                para.Value = ctpdcDTO.DonGiaNhap;

                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                mess = "Lỗi thêm phiếu";
                String e = ex.ToString();
            }
            finally
            {
                conn.Close();
            }
            return mess;
        }
    }
}
